package com.example.administrator.swredeem.utils;

import android.content.Context;


import android.device.IccManager;
import android.util.Log;
import android.widget.Toast;

public class ReadCard {
    public IccManager iccManager = new IccManager();

    /**
     * 读卡
     */
    public void readIC(Context context) {
        int status;
        byte[] type = new byte[1];
        type[0] = 0;

        //检测到0卡;如果未检测到卡，则为负数
        if (iccManager.detect() == 0) {
            //判断卡类型
            status = iccManager.request_Type(type);
            if (status == 0) {
                Toast.makeText(context, type[0] + "", Toast.LENGTH_SHORT).show();

                //打开读卡器
                status = iccManager.open((byte) 0, type[0], (byte) 1); //不同的读卡器 open方法参数不同
                if (status == 0) {
                    Toast.makeText(context, "成功打开读卡器", Toast.LENGTH_SHORT).show();
                    Log.d("TAG", "readCard: 成功打开读卡器" + status);
                    /**
                     * iccManager.Get1608ConfigZoneData();
                     */
                    byte[] buf = new byte[128];
                    boolean ptype = iccManager.Get1608ConfigZoneData(0, 128, buf);
                    Log.d("TAG", "readCard: " + ptype + "/n=========" + buf[0] + "\\" + buf[1] + "\\" + buf[2] + "\\" + buf[3]);
                    Log.d("TAG", "readCard: " + bytesToHexString(buf, 0, 128));

                    //7区读密码 60c9fe
                    //验证7区读密码
                    byte[] pwd = hexStringToByteArray("60c9fe");
                    boolean qreadpwd = iccManager.Verify1608ReadKey(7, pwd.length, pwd);
                    Log.d("TAG", "readIC:验证七区读密码 " + qreadpwd + "七区密码长度" + pwd.length);
                    if(qreadpwd){
                        //读取七区数据
                        byte[] qdata = new byte[255];
                        iccManager.ReadDataOf1608Card(7,0,qdata.length,qdata);
                        Log.d("TAG", "readCard: 获取的七区数据 " + bytesToHexString(qdata, 0, qdata.length));
//
                        //读取二区数据
                        //验证二区读密码 533bc5 1ba09b
                        //验证二区读密码 72677d 54b4a4
                        byte[] epwd = hexStringToByteArray("533bc5");
                        boolean ereadpwd = iccManager.Verify1608ReadKey(2, epwd.length, epwd);
                        Log.d("TAG", "readIC: 验证二区读密码 " + ereadpwd + "二区密码长度" + epwd.length);
                        if(ereadpwd){
                            byte[] edata = new byte[255];
                            iccManager.ReadDataOf1608Card(2,0,edata.length,edata);
                            Log.d("TAG", "readCard: 获取的二区数据" + bytesToHexString(edata, 0, edata.length));

                        }
                    }

//                    for (byte e : buf) {
//                        System.out.print(e + " ");
//                    }
//                    String str2 = new String(buf);

//                    byte[] atr = new byte[64];
//                    //激活并重置
//                    status = iccManager.activate(atr);
//                    if (status == 0) {
//                        Toast.makeText(MainActivity.this, "重置成功", Toast.LENGTH_SHORT).show();
//                        Log.d("TAG", "readCard: 重置成功" + Convert.bytesToHexString(atr, 0, status));
//                    } else {
//                        Toast.makeText(MainActivity.this, "重置失败", Toast.LENGTH_SHORT).show();
//                        Log.d("TAG", "readCard: 重置失败" + status);
//                    }
                } else {
                    Toast.makeText(context, "打开读卡器失败", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(context, "未检测到卡类型", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(context, "请插入IC卡", Toast.LENGTH_SHORT).show();
        }
    }


    //2进制转16进制
    public static String bytesToHexString(byte[] src, int offset, int length) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = offset; i < length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    //读密码
    public void read1608() {

    }

    //十六进制字符串转字节数组
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }

}
